using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;

namespace CMS.Presenters.Presenters
{
    public class NguoiSuDungPresenter
    {
        private List<NguoiSuDung> objList;
        private INguoiSuDungView iObjView;
        private INguoiSuDungDao iObjDAO;
        private IPhongBanDao pbDao;
        
        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public NguoiSuDungPresenter(INguoiSuDungView iObjView, INguoiSuDungDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("NguoiSuDungDao may not be null");
            
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;
            this.pbDao = new PhongBanDao();

            SubscribeViewToEvents();
        }

        public NguoiSuDungPresenter() { 
        
        }


        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
             LoadViewFromModel();
        }

        public void LoadViewFromModel()
        {
            //this.objList = this.iObjDAO.GetAllOrderField("TenNguoiSuDung", true);
            this.objList = this.iObjDAO.GetAllOrderField("ThuTu", true);
            iObjView.NguoiSuDungList = objList;
        }

        public PhongBan getTenPhongBan(string code)
        {
            return this.pbDao.GetById(code, false);
        }

        //Check stand for Phong ban
        [AjaxMethod]
        public string[] LoadStandForNguoiNhanVB(string keyword)
        {
            NguoiSuDung obj = null;
            this.iObjDAO = new NguoiSuDungDao();
            obj = this.iObjDAO.GetNguoiSuDungOfVietTat(keyword);
            string[] arr = new string[] { };
            if (obj != null)
            {
                arr = new string[] { obj.TenNguoiSuDung, obj.ID, obj.TenTruyCap, obj.PhongBan.TenPhongBan, obj.PhongBan.ID };
            }
            return arr;
        }

        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(string code)
        {
            //this.iObjDAO.Delete(iObjDAO.GetById(code, false));
            NguoiSuDung obj = iObjDAO.GetById(code, false);
            obj.TrangThai = false;
            this.iObjDAO.SaveOrUpdate(obj);
        }

        // For Update button
        public void ObjToUpdate(string code)
        {
            NguoiSuDung obj = iObjDAO.GetById(code, false);
            obj.TrangThai= !obj.TrangThai;
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
